package com.projecy.product.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.projecy.product.dto.MaterialDto;
import com.projecy.product.config.Number;
import com.projecy.product.model.ProductFile;
import com.projecy.product.model.ProductMaterial;
import com.projecy.product.model.ProductMaterialDetail;
import com.projecy.product.vo.ProductMaterialVo;
import com.projecy.product.service.ProductFileService;
import com.projecy.product.service.ProductMaterialDetailService;
import com.projecy.product.service.ProductMaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class ProductMaterialController {
    @Autowired
    private ProductMaterialService productMaterialService;
    @Autowired
    private ProductMaterialDetailService productMaterialDetailService;
    @Autowired
    private ProductFileService productFileService;


    /*点击制定物料组成查询出已审核类型为成品的数据*/
    @GetMapping("/selectProductFileNotAuditMark")
    public List<ProductFile> selectProductFileNotAuditMark(){
        QueryWrapper<ProductFile> wrapper = new QueryWrapper<>();
        wrapper.eq("audit_mark","已审核").eq("type","商品");
        return productFileService.list(wrapper);
    }

    /*点击添加物料查询出类型为物料的数据*/
    @GetMapping("/selectProductFileType")
    public List<ProductFile> selectProductFileType(){
        QueryWrapper<ProductFile> wrapper = new QueryWrapper<>();
        wrapper.eq("type","物料").eq("audit_mark","已审核");
        return productFileService.list(wrapper);
    }

    /*制定物料组成设计单*/
    @PostMapping("/insertProductMateial")
    public Boolean insertProductMateial(@RequestBody ProductMaterialVo productMaterialVo){
        Long didStr = Number.materialStock();
        productMaterialVo.setDid(didStr.toString());
        return productMaterialService.insertProductMaterial(productMaterialVo);
    }

    /*点击删除物料根据序号删除*/
    @GetMapping("/deleteProductMateialById/{did}")
    public Boolean deleteProductMateialById(@PathVariable String did){
        return productMaterialDetailService.removeById(did);
    }

    /*点击物料组成设计审核查询出未审核的数据*/
    @GetMapping("/selectNotProductMaterial")
    public List<ProductMaterial> selectNotproductMaterial(){
        QueryWrapper<ProductMaterial> wrapper = new QueryWrapper<>();
        wrapper.eq("audit_mark","未审核");
        return productMaterialService.list(wrapper);
    }

    /*点击审核执行同意根据id将审核改为已审核*/
    @GetMapping("/productMaterialAuditMark/{id}")
    public boolean productMaterialAuditMark(@PathVariable int id){
        UpdateWrapper<ProductMaterial> wrapper = new UpdateWrapper<>();
        wrapper.set("audit_mark","已审核").eq("id",id);
        return productMaterialService.update(wrapper);
    }

    /*点击物料组成设计单查询根据条件查询*/
    @GetMapping("/selectProductMaterial/selectPage")
    public IPage<ProductMaterial> selectProductMaterial(@PathVariable MaterialDto materialDto){
        Page p=new Page();
        QueryWrapper<ProductMaterial> wrapper = new QueryWrapper<>();
        wrapper.eq(!ObjectUtil.isEmpty(materialDto.getDetailsId()),"product_id",materialDto.getDetailsId());
        wrapper.eq(!ObjectUtil.isEmpty(materialDto.getDetailsName()),"product_name",materialDto.getDetailsName());
        p.setCurrent(materialDto.getCurrent());
        p.setSize(materialDto.getSize());
        return productMaterialService.page(p,wrapper);
    }

    /*点击物料组成设计单变更查询出已审核的数据*/
    @GetMapping("/selectProductMaterialAuditMark")
    public List<ProductMaterial> selectProductMaterialAuditMark(){
        QueryWrapper<ProductMaterial> wrapper = new QueryWrapper<>();
        wrapper.eq("audit_mark","已审核");
        return productMaterialService.list(wrapper);
    }
    /*点击变更根据产品ID进行查询物料详细表*/
    @GetMapping("/selectProductMaterialDetail/{pid}")
    public List<ProductMaterialDetail> selectProductMaterialDetail(@PathVariable String pid){
        QueryWrapper<ProductMaterialDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("prid",pid);
        return productMaterialDetailService.list(wrapper);
    }

    /*点击添加物料根据前端传的产品Id进行添加*/
    @PostMapping("/saveProductMaterialDetail/{pid}")
    public boolean saveProductMaterialDetail(@PathVariable String pid,@PathVariable ProductMaterialDetail productMaterialDetail){
        productMaterialDetail.setProductId(pid);
        productMaterialDetail.setSubtotal(productMaterialDetail.getPrice()* productMaterialDetail.getNumber());
        return  productMaterialDetailService.save(productMaterialDetail);
    }

    /*点击删除物料根据序号删除*/
    @GetMapping("deleteByProductMaterId/{id}")
    public boolean deleteByProductMaterId(@PathVariable int id){
        return productMaterialDetailService.removeById(id);
    }

}
